//
//  GLTableViewCellForButton.m
//  ttpod
//
//  Created by apple on 16/3/22.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "GLTableViewCellForButton.h"
#import "YPCollectionViewCellForFoundButton.h"
#import "YPModelForFound.h"
#import "Header.h"

@interface GLTableViewCellForButton () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain)UICollectionView *collection;
@property (nonatomic, retain)UIView *viewBlack;
@property (nonatomic, retain)YPModelForFound *model;
@property (nonatomic, retain)NSMutableArray *arrDic;

@end


@implementation GLTableViewCellForButton

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self createSubview];
    }
    
    return self;
}

- (void)createSubview {
    
    self.viewBlack = [[UIView alloc] init];
    self.viewBlack.backgroundColor = [UIColor colorWithRed:190 / 255.f green:190 /255.f blue:190 /255.f alpha:1];
    [self addSubview:self.viewBlack];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake((self.frame.size.width - 80) / 4, 50);
    flowLayout.minimumLineSpacing = 0;
    
    // 通过手机型号进行适配.
    if (IS_IPHONE4s || IS_IPHONE5) {
        
        flowLayout.minimumInteritemSpacing = 10;
    }else {
        
        flowLayout.minimumInteritemSpacing = 20;
    }
    
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 20, 5, 20);
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
    self.collection.bounces = NO;
    self.collection.dataSource = self;
    self.collection.delegate = self;
    [self.contentView addSubview:self.collection];
    self.collection.backgroundColor = [UIColor whiteColor];
    
    [self.collection registerClass:[YPCollectionViewCellForFoundButton class] forCellWithReuseIdentifier:@"poolForFoundButton"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = self.arrDic[indexPath.item];
    
    YPCollectionViewCellForFoundButton *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForFoundButton" forIndexPath:indexPath];
    
    [cell passModelForFoundButtonCol:dic];
    
    return cell;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    self.viewBlack.frame = CGRectMake(0, self.frame.size.height - 1, self.frame.size.width, 1);
    self.collection.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}

- (void)passModelForFoundButtonTal:(YPModelForFound *)model {
    
    self.arrDic = [NSMutableArray array];
    
    for (NSDictionary *dic in model.data) {
        
        [self.arrDic addObject:dic];
    }
    
    [self.collection reloadData];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = self.arrDic[indexPath.item];
    
    NSLog(@"%@", [dic objectForKey:@"id"]);
}







- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
